var app = new Vue({
	el: '#app',
	data: {
		isLogin: false,
		needReg: false,
		needLogin: true,
		needHead: true,
		needFoot: false,
		hasData: false,
		hasOrder: false,
		onHome: false,
		onOrder: false,
		onMy: false,
		onSequ: false,
		onPay: false,
		onUpdPwd: false,
		username: '',
		password: '',
		againPassword: '',
		newPassword: '',
		money: '',
		userId: '',
		addMoney: '',
		sta: '【点击选择】',
		staId: '',
		date: '【点击选择】',
		stas: {},
		stasArr: [],
		sequArr: [],
		day: '',
		fullDate: '',
		sequId: '',
		startTime: '',
		endTime: '',
		startSta: '',
		endSta: '',
		price: '',
		num: '1',
		payMoney: '',
		orderId: '',
		orders: []
	},
	methods: {
		showSta: function() {
			var that = this;
			var picker = new mui.PopPicker();
			picker.setData(that.stasArr);
			picker.show(function(selectItems) {
				that.sta = selectItems[0].text;
				that.staId = selectItems[0].value;
			})
		},
		showDate: function() {
			var that = this;
			new mui.DtPicker({
				"type": "date"
			}).show(function(selectItems) {
				that.date = selectItems.m.value + "月" + selectItems.d.value + "日";
				that.day = selectItems.m.value + "-" + selectItems.d.value;
				that.fullDate = new Date().getFullYear() + '-' + that.day;
			});
		},
		showNum: function() {
			var that = this;
			var picker = new mui.PopPicker();
			picker.setData([{
				text: '1',
				value: '1'
			}, {
				text: '2',
				value: '2'
			}, {
				text: '3',
				value: '3'
			}, {
				text: '4',
				value: '4'
			}, {
				text: '5',
				value: '5'
			}, {
				text: '6',
				value: '6'
			}, {
				text: '7',
				value: '7'
			}, {
				text: '8',
				value: '8'
			}, {
				text: '9',
				value: '9'
			}]);
			picker.show(function(selectItems) {
				that.num = selectItems[0].value;
				that.payMoney = that.num * that.price
			})
		},
		showPay: function(sequId, sTime, eTime, sSta, eSta, price) {
			var that = this;
			that.onHome = false,
				that.onOrder = false,
				that.onMy = false,
				that.onSequ = false,
				that.onPay = true,
				that.needHead = false,
				that.needFoot = false,
				that.sequId = sequId,
				that.startTime = sTime,
				that.endTime = eTime,
				that.startSta = sSta,
				that.endSta = eSta,
				that.price = price,
				that.payMoney = price;
			let params = new URLSearchParams();
			params.append("userId", that.userId);
			params.append("startStation", that.startSta);
			params.append("endStation", that.endSta);
			params.append("amount", that.price);
			params.append("startData", new Date(that.fullDate + " " + that.startTime + ":00"));
			axios({
					method: 'post',
					url: 'http://localhost:8080/bus/order/createOrder',
					data: params
				})
				.then(function(response) {
					that.orderId = response.data.orderId
				}, function(err) {
					mui.alert('系统异常，请稍后重试', '警告');
				});
		},
		login: function() {
			var that = this;
			if (that.username == '') {
				mui.toast('用户名不能为空');
			} else if (that.password == '') {
				mui.toast('密码不能为空');
			} else {
				let params = new URLSearchParams();
				params.append("username", that.username)
				params.append("password", that.password)
				axios({
						method: 'post',
						url: 'http://localhost:8080/bus/user/login',
						data: params
					})
					.then(function(response) {
						if (response.data.user != null) {
							mui.toast('登录成功');
							that.isLogin = true,
								that.needLogin = false;
							that.goHome();
							that.needFoot = true,
								that.userId = response.data.user.userId,
								that.money = response.data.user.money;
							axios.get("http://localhost:8080/bus/busLine/lineJson")
								.then(function(response) {
									for (var i = 0; i < response.data.line.length; i++) {
										that.stas = {}
										that.stas.value = response.data.line[i].lineId;
										that.stas.text = response.data.line[i].fullName;
										that.stasArr[i] = that.stas;
									}
								}, function(err) {
									mui.alert('系统异常，请稍后重试', '警告');
								});
						} else if (response.data.code == 2) {
							mui.alert('用户名或密码不能为空', '提示');
						} else {
							mui.alert('用户名不存在或密码错误', '提示');
						}
					}, function(err) {
						mui.alert('系统异常，请稍后重试', '警告');
					});
			}
		},
		selSequ: function() {
			var that = this;
			that.sequArr = [];
			axios.get("http://localhost:8080/bus/sequence/sequJson?date=" + that.day + "&lineId=" +
					that.staId)
				.then(function(response) {
					if (response.data.sequ.length != 0) {
						for (var i = 0; i < response.data.sequ.length; i++) {
							that.sequArr[i] = response.data.sequ[i]
						}
						that.hasData = true;
						that.goSequ();
					} else {
						mui.alert('符合条件的所有班次均已发车', '提示');
					}
				}, function(err) {
					mui.alert('线路查询失败，请稍后重试', '警告');
				});
		},
		reg: function() {
			var that = this;
			if (that.username == '') {
				mui.toast('用户名不能为空');
			} else if (that.password == '' || that.againPassword == '') {
				mui.toast('密码不能为空');
			} else {
				if (that.password == that.againPassword) {
					let params = new URLSearchParams();
					params.append("username", that.username)
					params.append("password", that.password)
					axios.post("http://localhost:8080/bus/user/reg", params)
						.then(function(response) {
							if (response.data.code == 1) {
								mui.toast('注册成功');
								that.needReg = false,
									that.needLogin = true,
									that.againPassword = '';
							} else if (response.data.code == 0) {
								mui.alert('用户名已存在', '提示');
							} else if (response.data.code == 2) {
								mui.alert('用户名或密码不能为空', '提示');
							}
						}, function(err) {
							mui.alert('系统异常，请稍后重试', '警告');
						});
				} else {
					mui.toast('两次密码不一致，请重新输入');
					that.password = '';
					that.againPassword = '';
				}
			}
		},
		updPwd: function() {
			var that = this;
			if (that.password == '' || that.againPassword == '' || that.newPassword == '') {
				mui.toast('密码不能为空');
			} else {
				if (that.newPassword == that.againPassword) {
					let params = new URLSearchParams();
					params.append("password", that.newPassword)
					params.append("userId", that.userId)
					params.append("oldPassword", that.password)
					axios.post("http://localhost:8080/bus/user/updPwd", params)
						.then(function(response) {
							if (response.data.code == 1) {
								that.isLogin = false,
									that.needLogin = true,
									that.userId = '',
									that.username = '',
									that.password = '',
									that.newPassword = '',
									that.againPassword = '',
									that.money = '',
									that.needFoot = false,
									that.sta = '【点击选择】',
									that.date = '【点击选择】',
									staId = '';
								mui.toast('密码修改成功，请重新登录');
							} else if (response.data.code == 0) {
								that.password = '',
									that.newPassword = '',
									that.againPassword = '';
								mui.alert('您输入的原密码有误', '提示');
							}
						}, function(err) {
							mui.alert('系统异常，请稍后重试', '警告');
						});
				} else {
					mui.toast('两次密码不一致，请重新输入');
					that.newPassword = '';
					that.againPassword = '';
				}
			}
		},
		pay: function() {
			var that = this;
			axios.get("http://localhost:8080/bus/order/pay?orderId=" + that.orderId + "&userId=" +
					that.userId + "&amount=" + that.payMoney)
				.then(function(response) {
					if (response.data.code == 1) {
						mui.alert('支付成功！', '提示');
					} else if (response.data.code == 0) {
						mui.alert('余额不足，未支付！', '提示');
					} else if (response.data.code == 2) {
						mui.alert('触发手动异常', '警告');
					}
					that.needHead = true,
						that.needFoot = true,
						that.onPay = false,
						that.num = '1';
					that.goOrder();
				}, function(err) {
					mui.alert('系统异常，请稍后重试', '警告');
				});
		},
		payAgain: function(orderId, payMoney) {
			var that = this;
			axios.get("http://localhost:8080/bus/order/pay?orderId=" + orderId + "&userId=" +
					that.userId + "&amount=" + payMoney)
				.then(function(response) {
					if (response.data.code == 1) {
						mui.alert('支付成功！', '提示');
					} else if (response.data.code == 0) {
						mui.alert('余额不足，支付失败！', '提示');
					} else if (response.data.code == 2) {
						mui.alert('触发手动异常', '警告');
					}
					that.needHead = true,
						that.needFoot = true,
						that.onPay = false,
						that.goOrder();
				}, function(err) {
					mui.alert('系统异常，请稍后重试', '警告');
				});
		},
		getMoney: function() {
			var that = this;
			mui.prompt('请输入充值金额:', '金额', '充值', function(e) {
				if (e.index == 1) {
					that.addMoney = e.value;
					axios.get("http://localhost:8080/bus/user/getMoney?userId=" + that
							.userId + "&addMoney=" + that.addMoney)
						.then(function(response) {
							if (response.data.code == 1) {
								mui.alert('充值成功', '提示');
								that.money = (that.money * 10000 + that.addMoney * 10000) /
									10000
							}
						}, function(err) {
							mui.alert('系统异常，请稍后重试', '警告');
						});
				} else {
					mui.alert('你取消了充值', '提示');
				}
			})
		},
		goLogin: function() {
			this.needReg = false,
				this.needLogin = true
		},
		goReg: function() {
			this.needReg = true,
				this.needLogin = false
		},
		goHome: function() {
			this.onHome = true,
				this.onOrder = false,
				this.onMy = false,
				this.onSequ = false;
			this.onUpdPwd = false;
		},
		goOrder: function() {
			var that = this;
			that.orders = []
			that.onHome = false,
				that.onOrder = true,
				that.onMy = false
			that.onSequ = false,
				that.onUpdPwd = false;
			axios.get("http://localhost:8080/bus/order/selOrder?userId=" + that.userId)
				.then(function(response) {
					that.orders = response.data.order;
					if (that.orders.length != 0) {
						that.hasOrder = true;
						for (var i = 0; i < that.orders.length; i++) {
							that.orders[i].createTime = that.formatDate(that.orders[i].createTime);
							that.orders[i].startData = that.formatDate(that.orders[i].startData);
						}
					}
				}, function(err) {
					mui.alert('系统异常，请稍后重试', '警告');
				});
		},
		formatDate: function(date, fmt = "yyyy-MM-dd hh:mm:ss") {
			date = new Date(date);
			var o = {
				'M+': date.getMonth() + 1,
				'd+': date.getDate(),
				'h+': date.getHours(),
				'm+': date.getMinutes(),
				's+': date.getSeconds(),
				'q+': Math.floor((date.getMonth() + 3) / 3),
				'S': date.getMilliseconds(),
			}
			if (!date || date == null) return null;
			if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp
				.$1.length))
			for (var k in o) {
				if (new RegExp('(' + k + ')').test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr((
						'' + o[k]).length)));
				}
			}
			return fmt;
		},
		goMy: function() {
			var that = this;
			that.onHome = false,
				that.onOrder = false,
				that.onMy = true,
				that.onSequ = false,
				that.onUpdPwd = false;
			axios.get("http://localhost:8080/bus/user/selMoney?userId=" + that.userId)
				.then(function(response) {
					that.money = response.data.money;
				}, function(err) {
					mui.alert('系统异常，请稍后重试', '警告');
				});
		},
		goSequ: function() {
			this.onHome = false,
				this.onOrder = false,
				this.onMy = false
			this.onSequ = true
			this.onUpdPwd = false
		},
		goUpdPwd: function() {
			this.password = '',
				this.againPassword = '',
				this.newPassword = '',
				this.onHome = false,
				this.onOrder = false,
				this.onMy = false
			this.onSequ = false,
				this.onUpdPwd = true
		},
		logout: function() {
			var that = this;
			that.isLogin = false,
				that.needLogin = true,
				that.userId = '',
				that.username = '',
				that.password = '',
				that.newPassword = '',
				that.againPassword = '',
				that.money = '',
				that.needFoot = false,
				that.sta = '【点击选择】',
				that.date = '【点击选择】',
				that.staId = '';
			that.hasOrder = false
			mui.toast('退出成功');
		}
	},
	updated: function() {
		var gallery = mui('.mui-slider');
		gallery.slider({
			interval: 3000
		});
	}
})
